import json
import logging
from datetime import datetime
import requests

from app.db.database import SessionLocal
from app.db.crud import create_tpost
from app.db.models import TPost
from app.pacong.content_fetcher import ContentFetcher

logger = logging.getLogger(__name__)

class Kr36HotListFetcher(ContentFetcher):
    """36氪热榜爬虫"""

    def __init__(self):
        super().__init__()
        self.source = "36kr"
        self.source_name = "36氪热榜"
        self.content_type = 17  # 36氪的source_id设为17

    def fetch_content(self) -> list:
        """获取36氪热榜数据"""
        url = "https://gateway.36kr.com/api/mis/nav/home/nav/rank/hot"
        
        try:
            headers = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
                "Accept": "application/json, text/plain, */*",
                "Origin": "https://www.36kr.com",
                "Referer": "https://www.36kr.com/",
                "Content-Type": "application/json;charset=UTF-8",
                "Host": "gateway.36kr.com"
            }
            
            # 36氪API需要的请求体
            payload = {
                "partner_id": "web",
                "timestamp": int(datetime.now().timestamp() * 1000),
                "param": {
                    "siteId": 1,
                    "platformId": 2
                }
            }
            
            response = requests.post(url, headers=headers, json=payload)
            response.raise_for_status()
            data = response.json()
            
            if data.get("code") != 0:
                logger.error(f"36氪接口返回错误: {data.get('msg', '未知错误')}")
                return []
            
            # 处理响应数据
            hot_list = []
            for item in data.get("data", {}).get("hotRankList", []):
                if "templateMaterial" not in item:
                    continue
                hot_item = {
                    "title": item["templateMaterial"]["widgetTitle"],
                    "url": f"https://www.36kr.com/p/{item['itemId']}",
                    "pub_date": datetime.fromtimestamp(item["templateMaterial"].get("publishTime", 0) / 1000),
                    "content": item["templateMaterial"].get("widgetContent", ""),
                    "short_content": item["templateMaterial"].get("summary", "")
                }
                hot_list.append(hot_item)
            
            return hot_list
            
        except Exception as e:
            logger.error(f"获取36氪热榜数据失败: {str(e)}")
            return []


if __name__ == "__main__":
    # 测试代码
    fetcher = Kr36HotListFetcher()
    fetcher.fetch_and_save() 